﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.Net.Mail;

namespace AutoSendMail
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                try
                {
                    string filename = Environment.CurrentDirectory + "\\测试数据.txt";
                    Console.WriteLine("准备发送邮件带附件:" + filename);
                    Console.ReadKey();
                    SendMail(filename);
                    Console.WriteLine("邮件发送成功！");
                }
                catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            }
        }

        private static bool SendMail(string AttachmentsName)
        {
            System.IO.FileInfo finfo = new System.IO.FileInfo(AttachmentsName);
            MailMessage CurMail = new MailMessage();
            CurMail.From = new MailAddress("tmptest@hnlx.com.cn", finfo.Name, Encoding.Default);
            CurMail.To.Add("tmptest@hnlx.com.cn");

            CurMail.Attachments.Add(new Attachment(AttachmentsName));

            SmtpClient smtp = new SmtpClient("smtp.ym.163.com");
            smtp.Credentials = new System.Net.NetworkCredential("tmptest@hnlx.com.cn", "tmptest");
            smtp.Send(CurMail);
            return false;
        }
    }
}
